package com.acmedcare.framework.exchange.center.core.handler;

import com.acmedcare.framework.exchange.center.core.JmxContainerUtil;
import com.acmedcare.framework.exchange.center.core.exception.PluginNotFoundException;
import com.acmedcare.framework.exchange.center.entity.bean.Consumer;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class ConsumerHandlerProxy implements MethodInterceptor {

  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
      throws Throwable {
    if (method.getName().equals("invoke")) {
      Consumer consumer = (Consumer) objects[1];
      ConsumerHandlerMBean handlerMBean = JmxContainerUtil.ConsumerHandlers
          .get(consumer.getPluginName());
      if (handlerMBean == null) {
        throw new PluginNotFoundException(
            String.format("插件:%s,尚未找到，请确认插件已经正确加载!", consumer.getPluginName()));
      }
      handlerMBean.invoke(objects[0].toString(), consumer);
    }
    return null;
  }
}
